package acs.offer.server.service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.alibaba.fastjson.JSON;

import acs.offer.collection.Params;
import acs.offer.constant.Constant;
import acs.offer.constant.EventType;
import acs.offer.controller.EventBus;
import acs.offer.pojo.Message;
import acs.offer.pojo.RequestEnum;
import acs.offer.pojo.ResponseEnum;
import acs.offer.pojo.TranstractDate;
import acs.offer.util.DMSSoftTimer;
import acs.offer.util.DbUtil;
import acs.offer.util.DmsRequestUtil;
import acs.offer.util.ServerCache;

public class DmsService {
	
	private static final int STATE_INIT = 1;
	private static final int STATE_WAITING_OFFER = 2;// 加载数据中
	private static final int STATE_TIME_OUT = 3;
	private static final int STATE_DATA_LOADED = 4;
	private int currentState = STATE_INIT;// 状态
	
	private static DmsService instance=new DmsService();
	
	public static DmsService getInstance() {
		return instance;
	}
	 /**
        * 移除缓存
     * @param param
     */
	public void clearCache(Params param) {
		Message message = param.get(Constant.MESSAGE);
		TranstractDate transtract = JSON.parseObject(message.getContent(), TranstractDate.class);
		ServerCache.getInstance().remove(transtract.getRequestEnum().toString());
	}

	public void sendResponse(Params param) {
		if(this.currentState == STATE_TIME_OUT) {//说明是第一次time_out 不用给客户端响应了
			this.currentState = STATE_DATA_LOADED;//下一次可以响应了
			return;
		}
		TranstractDate response = TranstractDate.build();
		response.setData(param.get(Constant.TRANSFER_OFFER));
		response.setResponseEnum(ResponseEnum.OFFER_RESPONSE);// 这里应该也是动态的，这里暂时写死
		DmsRequestUtil.request(Message.build(JSON.toJSONString(response), param.get(Constant.SESSION_ID)));
		this.currentState = STATE_DATA_LOADED;
	}

	public void handleRequest(Params param) {
		switch (this.currentState) {
		case STATE_INIT:
			Message message = param.get(Constant.MESSAGE);
			TranstractDate transtract = JSON.parseObject(message.getContent(), TranstractDate.class);
			RequestEnum requestEnum = transtract.getRequestEnum();
			switch (requestEnum) {
			case REQUEST_OFFER:
				System.out.println("要去数据库加载offer了");
				Map<String, Object> offers = ServerCache.getInstance().get(ServerCache.OFFER_CACHE_KEY);// 这里就是相当于从task中获取数据
				// 这里可以用异步加载的方式，加载完将offers放入params，并给messagequeue发送一条处理返回offer的消息
				// CompletableFuture.runAsync , 只是感觉耦合了
				if (offers == null) {
					CompletableFuture.runAsync(() -> {
						DbUtil.getInstance().loadOffer(param, transtract);
					});
					DMSSoftTimer.getInstance().startTimer(5 * 1000, EventType.TIMEOUT_WHEN_GET_TARGET_OFFER,param);//加载数据是否超时
					this.currentState=STATE_WAITING_OFFER;//得到得到目标数据状态
				} else {
					param.put(Constant.TRANSFER_OFFER, offers);
					EventBus.getInstance().putMessage(EventType.RESPONSE_FOR_DATA, param);
					System.out.println("服务端查到了offer，现在开始给客户端返回offer信息");
					this.currentState=STATE_DATA_LOADED;//已经得到了数据
				}

				break;

			}
			break;
		case STATE_WAITING_OFFER:
			break;
		case STATE_DATA_LOADED:
			this.sendResponse(param);//直接返回数据
			break;
		}
		
	}
	/**
	 * 服务端获取数据超时了，不给客户端响应数据了
	 * @param param
	 */
	public void doNotResponseDataToClient(Params param) {
		this.currentState=STATE_TIME_OUT;
	}
}
